package com.renwei.yunlong.activity.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.FileDisplayActivity;
import com.renwei.yunlong.adapter.ImagePagerAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.event.FileExistEvent;
import com.renwei.yunlong.service.FileExistService;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.HackyViewPager;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by wangqi on 2017/10/20.
 */

public class PhotoDisplayActivity extends BaseActivity implements ViewPager.OnPageChangeListener, ImagePagerAdapter.ImagePagerClick, ImagePagerAdapter.OnFileClicklistener {


    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.view_pager)
    HackyViewPager viewPager;
    @Bind(R.id.ll_container)
    LinearLayout llContainer;
    //小红点移动的距离
    private int mPointDis;
    ImagePagerAdapter adapter;
    ArrayList<String> paths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_display);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.color_333333), 0);
        paths = (ArrayList<String>) getIntent().getSerializableExtra("paths");
        mPointDis = getIntent().getIntExtra("position", -1);
        toolbar.setBackgroundColor(getResources().getColor(R.color.color_333333));
        adapter = new ImagePagerAdapter(paths, this, this);
        adapter.setImagePagerClick(this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(mPointDis);
        initData();
    }

    private void initData() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closePager();
            }
        });
        viewPager.setOnPageChangeListener(this);
        for (int i = 0; i < paths.size(); i++) {
            ImageView point = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.leftMargin = 40;
            point.setLayoutParams(params);
            point.setBackgroundResource(R.drawable.point_selector);//设置背景
            llContainer.addView(point);
        }
        if (llContainer.getChildCount() <= 1) {
            llContainer.setVisibility(View.GONE);
        } else {
            llContainer.setVisibility(View.VISIBLE);
        }
        if (paths != null && paths.size() > 0) {
            toolbar.setTitle(String.valueOf(paths.get(mPointDis)));
            setCheckPager(mPointDis);
        }
    }

    //设置默认选中页面的标题和小白点
    public void setCheckPager(int position) {
        String path = paths.get(position);
        try {
            int i = path.lastIndexOf("/");
            toolbar.setTitle(String.valueOf(path.substring(i + 1, path.length())));
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < llContainer.getChildCount(); i++) {
            if (i == position) {
                llContainer.getChildAt(i).setEnabled(true);
            } else {
                llContainer.getChildAt(i).setEnabled(false);
            }
        }
    }

    public static void OpenActivity(Context context, ArrayList<String> paths, int position) {
        Intent intent = new Intent(context, PhotoDisplayActivity.class);
        intent.putExtra("paths", paths);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCheckPager(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    public void closePager() {
        this.finish();
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(FileExistEvent event) {
        if (event.isExist()) {
            FileDisplayActivity.openActivity(this, StringUtils.value(event.getPath()));
        } else {
            showTopWrongMsg("文件不存在");
        }
    }

    @Override
    public void onClick(String path) {
        startService(new Intent(mContext, FileExistService.class).putExtra("path", path));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
